<div class="bjui-pageContent" id="category_choose">
    <div class="bjui-pageHeader" style="background-color:#fefefe; border-bottom:none;">
        <form data-toggle="ajaxsearch" data-options="{searchDatagrid:$.CurrentDialog.find('#dg-dict-category-list')}">
            <fieldset>
                <div style="margin:0; padding:1px 5px 5px;">
                    <span>主分类：</span>
                    <select name="pid" id="pid_select" data-toggle="selectpicker" onchange="searchCategory(this.options[this.options.selectedIndex].value)">
                        <option value="0">所有分类</option>
                        @foreach ($role_list as $val)
                            <option  {{$val['selected']}} value="{{$val['id']}}">{{$val['cat_name']}}</option>
                        @endforeach
                    </select>
                    <input type="hidden" id="field_id" value="{{$field_id}}">
                    <input type="hidden" id="children_category_id" value="{{$children_category_id}}">
                     <button type="button" class="btn-green" data-icon="save" onClick="saveInfo();">保存</button>
                </div>
            </fieldset>
        </form>
    </div>
    <table id="dg-dict-category-list" data-toggle="datagrid" data-options="{
        width: '100%',
        height: '100%',
        gridTitle: '',
        showToolbar: false,
        toolbarItem:false,
        showCheckboxcol: true,
        {{--editMode: 'dialog',--}}
        local: 'local',
        dataUrl: 'admin/process/dict-category-list',
        postData:{
            pid:{{$old_category_id}},
        },
        inlineEditMult: false,
        isTree: 'cat_name',
        addLocation:false,
        fieldSortable: false,
        columnMenu: false,
        paging: false,
        treeOptions:{
                       keys        : {
                           key       : 'id',        // id
                           parentKey : 'pid',       // 父id
                           childKey  : 'children',  // 子列表数据key值
                           childLen  : 'childlen',  // 子列表数据条数
                           isParent  : 'isparent',  // 是否父数据
                           level     : 'level',     // 层级
                           order     : 'order',     // 顺序
                           isExpand  : 'isExpand'   // 子列表是否展开
                       },
                       simpleData  : true,          // 是否简单数据(array格式)
                       add         : false           // 显示添加按钮
                   },
        filterThead:false,
    }">
        <thead>
        <tr>
            {{--<th data-options="{width:50,render:draw_checkbox}">选择</th>--}}
            <th data-options="{name:'cat_name', align:'center', width:300, rule:'required'}">分类名称</th>
        </tr>
        </thead>
    </table>
</div>>

<script type="text/javascript">
    function saveInfo(){
          var pid = $('#pid_select').val();
          var field_id = $('#field_id').val();
          var cids = '';
          var cid_arr = [];
          var data = $('#dg-dict-category-list').data('selectedDatas');

          if(data){
             $.each(data,function(i,item){
                    cid_arr[i] = item['id'];
              });

              var cids = cid_arr.join(',');
          }

           $.ajax({
              type: "POST",
              url: "admin/process/dict-category-list-ajax",
              data: {
                pid:pid,
                cids:cids,
                field_id:field_id
              },
              success: function(data){
                    BJUI.dialog('close', 'process-dict-category-dialog')
              }
           });
     }
    function searchCategory(){
        BJUI.ajax('ajaxsearch', {
            url: 'admin/process/dict-category-list',
            form: $.CurrentDialog.find('form:eq(0)'),
            validate: true,
            loadingmask: true,
            searchDatagrid:$('#j_datagrid_tree')
        })
    }

    $(document).on(BJUI.eventType.afterInitUI, function(event) {
        initCheckbox();
    });

    function initCheckbox(){
        var children_category_id = $('#children_category_id').val();
//        $('#dg-dict-category-list').datagrid('selectedRows',0);
        //console.log(children_category_id);
        if(children_category_id){
            children_category_id = children_category_id.split(',');
            var data = $('#dg-dict-category-list').data('allData');

            for(j = 0; j < children_category_id.length; j++) {
               for(i = 0; i<data.length;i++){
                   if(data[i].id == children_category_id[j]){
                        $('#dg-dict-category-list').datagrid('selectedRows',i);
                   }
               }
            }
        }
    }
</script>